package com.example.mytestdemo.viewpage2

import android.util.Log
import android.view.View
import androidx.core.view.size
import androidx.viewpager2.widget.ViewPager2
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityViewpager2ListadapterLayoutBinding
import com.example.mytestdemo.viewpage2.adapter.*

class ViewPager2ListAdapterActivity :
    BaseViewBindActivity<ActivityViewpager2ListadapterLayoutBinding>() {

    companion object {
        private const val TAG = "ViewPager2ListAdapterAc"
    }

    override fun getLayoutInflateId(): Int {
        return R.layout.activity_viewpager2_listadapter_layout
    }

    override fun initData() {

    }

    override fun initUI() {
//        initViewPager2Adapter()

        initTestCustomFrameLayoutPager2Adapter()


    }
    /**
     *自定义xml布局
     */
    private fun initTestCustomFrameLayoutPager2Adapter(){
        mBind.viewpager2.visibility = View.GONE
        mBind.testCustomFramelayout.visibility = View.VISIBLE
        mBind.testCustomFramelayout.initCustomFrameLayout()
    }

    private fun initViewPager2Adapter() {
        mBind.viewpager2.visibility = View.VISIBLE
        mBind.testCustomFramelayout.visibility = View.GONE


        val data = arrayListOf<String>()
        (0..10).forEach {
            data.add("数据$it")
        }
//        mBind.viewpager2.offscreenPageLimit = 1
//        val myAdapter = MyViewPager2Adapter(object : ImyAdapterListener {
//            override fun listener(position: Int) {
//                Log.d(TAG, "tgw listener: $position")
//            }
//
//        })//MyViewPager2Adapter，普通加载文本

        val myAdapter = MyViewPager2RecycleAdapter(object : ImyAdapterListener {
            override fun listener(position: Int) {
                Log.d(TAG, "tgw listener: $position")
            }

        })//嵌套recyclerview
//        val myAdapter = MyTestAdapter()//封装的MyViewPager2Adapter
        myAdapter.setList(data)

//        val myAdapter = MyViewPager2DiffutilAdapter()//
//        myAdapter.submitList(data)

        mBind.viewpager2.adapter = myAdapter
        mBind.viewpager2.setCurrentItem(2)
        myAdapter.notifyItemChanged(2)
        mBind.btRefresh.setOnClickListener{
//            data[0] = "局部刷新数据0"
//            //必须新建对象，原因看submitList源码，内部进行对象比对截断
//            var newData = arrayListOf<String>()
//            data.forEach{
//                newData.add(it)
//            }
//
//            myAdapter.submitList(newData)

            data.forEachIndexed{index,content->
                data[index] = "局部刷新数据$content"
            }
            myAdapter.notifyItemChanged(0)

//            myAdapter.notifyDataSetChanged()
        }

        mBind.viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                Log.d(TAG, "MyViewPager2Adapter onPageSelected: $position---${mBind.viewpager2.childCount}")
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }
}